import pygame, sys, math, random

pygame.init()

from Block import Block
from Player import Player
from HardBlock import HardBlock
from DiagonalBlockRight import DiagonalBlockRight
from DiagonalBlockLeft import DiagonalBlockLeft
from Background import Background

clock = pygame.time.Clock()

width = 800
height = 600
size = width, height

blocksize = [50,50]
playersize = [40,40]

screen = pygame.display.set_mode(size)
bgColor = r,g,b = 0,0,0

blocks = pygame.sprite.Group()
hardBlocks = pygame.sprite.Group()
diagonalBlocksRight = pygame.sprite.Group()
diagonalBlocksLeft = pygame.sprite.Group()
backgrounds = pygame.sprite.Group()
players = pygame.sprite.Group()
all = pygame.sprite.OrderedUpdates()

Player.containers = (all, players)
Block.containers = (all, blocks)
HardBlock.containers = (all, hardBlocks, blocks)
DiagonalBlockRight.containers = (all, diagonalBlocksRight, blocks)
DiagonalBlockLeft.containers = (all, diagonalBlocksLeft, blocks)
Background.containers = (all, blocks)


bg = Background("rsc/bg/mainbg.png", size)

def loadLevel(level):
    f = open(level+".lvl", 'r')
    lines = f.readlines()
    f.close()
    
    newlines = []
    
    for line in lines:
        newline = ""
        for c in line:
            if c != "\n":
                newline += c
        newlines += [newline]
        
    for line in newlines:
        print line
    
    for y, line in enumerate(newlines):
        for x, c in enumerate(line):
            if c == "s":
                Block("rsc/blocks/black.png", 
                      [(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2], 
                      blocksize)
            elif c == "d":
                HardBlock("rsc/blocks/purple.png", 
                      [(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2], 
                      blocksize)
            elif c == "h":
                DiagonalBlockRight("rsc/blocks/blue.png", 
                      [(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2], 
                      blocksize)    
            elif c == "m":
                HardBlock("rsc/blocks/red.png", 
                      [(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2], 
                      blocksize)
            elif c == "#":
                HardBlock("rsc/blocks/yellow.png",
                      [(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2], 
                      blocksize)
            elif c == "b":
                DiagonalBlockLeft("rsc/blocks/pink.png", 
                      [(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2], 
                      blocksize)
            elif c == "t":
                Block("rsc/blocks/tree.png", 
                      [(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2], 
                      blocksize)
    
    
    f = open(level+".tng", 'r')
    lines = f.readlines()
    f.close()
    
    newlines = []
    
    for line in lines:
        newline = ""
        for c in line:
            if c != "\n":
                newline += c
        newlines += [newline]
        
    for line in newlines:
        print line
    
    for y, line in enumerate(newlines):
        for x, c in enumerate(line):
            if c == "@":
                player = Player([(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2],
                     playersize,
                     size)
    for each in all.sprites():
        each.fixLocation(player.offsetx, player.offsety)

levels = ["rsc/levels/level1",
          "rsc/levels/level2"]
level = 0
loadLevel(levels[level])
player1 = players.sprites()[0]

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
                if level < len(levels)-1:
                    level += 1
                else:
                    level = 0
                for each in all.sprites():
                    each.kill()
                bg = Background("rsc/bg/mainbg.png", size)
                screen.blit(bg.image, bg.rect)
                loadLevel(levels[level])
                player1 = players.sprites()[0]
                
            if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                player1.direction("right")
            if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                player1.direction("left")
            #if event.key == pygame.K_w or event.key == pygame.K_UP:
                #player1.direction("up")
            #if event.key == pygame.K_s or event.key == pygame.K_DOWN:
                #player1.direction("down")
            if event.key == pygame.K_SPACE:
                player1.direction("jump")
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                player1.direction("stop right")
            if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                player1.direction("stop left")
            #if event.key == pygame.K_w or event.key == pygame.K_UP:
                #player1.direction("stop up")
            #if event.key == pygame.K_s or event.key == pygame.K_DOWN:
                #player1.direction("stop down")
    
    playersHitBlocks = pygame.sprite.groupcollide(players, hardBlocks, False, False)
    playersHitDiagonalBlocksRight = pygame.sprite.groupcollide(players, diagonalBlocksRight, False, False)
    playersHitDiagonalBlocksLeft = pygame.sprite.groupcollide(players, diagonalBlocksLeft, False, False)
    
    for player in playersHitDiagonalBlocksRight:
        for block in playersHitDiagonalBlocksRight[player]:
            player.collideDiagnalBlockRight(block)
    
    for player in playersHitDiagonalBlocksLeft:
        for block in playersHitDiagonalBlocksLeft[player]:
            player.collideDiagnalBlockLeft(block)
    
    for player in playersHitBlocks:
        for block in playersHitBlocks[player]:
            player.collideBlock(block)

    all.update(size,
               player1.speedx, 
               player1.speedy, 
               player1.scrollingx, 
               player1.scrollingy,
               player1.realx,
               player1.realy)
    
    dirty = all.draw(screen)
    pygame.display.update(dirty)
    pygame.display.flip()
    #clock.tick(30)'
    
    clock.tick(90)
